package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.SysMsgTemplate;
import com.ruoyi.system.domain.vo.SmsTemplateSendReqVO;
import com.ruoyi.system.service.ISysMsgSendService;
import com.ruoyi.system.service.ISysMsgTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 消息模板Controller
 *
 * @author Felix
 * @date 2022-04-14
 */
@Api(tags = " 消息模板")
@RestController
@RequestMapping("/sysMsgTemplate")
public class SysMsgTemplateController extends BaseController {
    @Autowired
    private ISysMsgTemplateService sysMsgTemplateService;
    @Autowired
    private ISysMsgSendService smsSendService;

    /**
     * 查询消息模板列表
     */
    @ApiOperation(value="查询消息模板列表", notes="查询消息模板列表")
    @PreAuthorize(hasPermi = "system:sysMsgTemplate:list")
    @GetMapping("/list")
    public TableDataInfo list(SysMsgTemplate sysMsgTemplate) {
        startPage();
        List<SysMsgTemplate> list = sysMsgTemplateService.selectSysMsgTemplateList(sysMsgTemplate);
        return getDataTable(list);
    }

    /**
     * 导出消息模板列表
     */
    @ApiOperation(value="导出消息模板列表", notes="导出消息模板列表")
    @PreAuthorize(hasPermi = "system:sysMsgTemplate:export")
    @Log(title = "消息模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMsgTemplate sysMsgTemplate) throws IOException {
        List<SysMsgTemplate> list = sysMsgTemplateService.selectSysMsgTemplateList(sysMsgTemplate);
        ExcelUtil<SysMsgTemplate> util = new ExcelUtil<SysMsgTemplate>(SysMsgTemplate.class);
        util.exportExcel(response, list, "消息模板数据");
    }
    /**
     * 获取消息模板详细信息
     */
    @ApiOperation(value="获取消息模板详细信息", notes="获取消息模板详细信息")
    @PreAuthorize(hasPermi = "system:sysMsgTemplate:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(sysMsgTemplateService.selectSysMsgTemplateById(id));
    }

    /**
     * 新增消息模板
     */
    @ApiOperation(value="新增消息模板", notes="新增消息模板")
    @PreAuthorize(hasPermi = "system:sysMsgTemplate:add")
    @Log(title = "消息模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMsgTemplate sysMsgTemplate) {
        return toAjax(sysMsgTemplateService.insertSysMsgTemplate(sysMsgTemplate));
    }

    //sendSms
    @PostMapping("/sendSms")
    @ApiOperation("发送消息")
    public AjaxResult sendSms(@RequestBody SmsTemplateSendReqVO sendReqVO){
        return toAjax(smsSendService.sendSingleSmsToAdmin(sendReqVO.getMobile(), null,
                sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
    }

    /**
     * 修改消息模板
     */
    @ApiOperation(value="修改消息模板", notes="修改消息模板")
    @PreAuthorize(hasPermi = "system:sysMsgTemplate:edit")
    @Log(title = "消息模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMsgTemplate sysMsgTemplate) {
        return toAjax(sysMsgTemplateService.updateSysMsgTemplate(sysMsgTemplate));
    }

    /**
     * 删除消息模板
     */
    @ApiOperation(value="删除消息模板", notes="删除消息模板")
    @ApiImplicitParam(name = "ids", value = "消息模板ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "system:sysMsgTemplate:remove")
    @Log(title = "消息模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysMsgTemplateService.deleteSysMsgTemplateByIds(ids));
    }
}
